package usf.gwt.ui.bootstrap.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;

public class AddOnTextBox extends TextBox {

	private static final String DEFAULT_GROUP_STYLE = "input-group";
	private static final String DEFAULT_GROUP_ADD_ON_STYLE = "input-group-addon";
	
	@Override
	protected Element initWidget() {
		Element e = Document.get().createDivElement();
		e.appendChild(Document.get().createSpanElement());
		e.appendChild(super.initWidget());
		e.appendChild(Document.get().createSpanElement());
		return e;
	}
	
	@Override
	protected void initStyle() {
		super.initStyle();
		getElement().setClassName(DEFAULT_GROUP_STYLE);
	}
	
	public void setLeftAddOn(IconTypes type) {
		setAddOn(getLeftAddOn(), type);
	}
	public void setRightAddOn(IconTypes type) {
		setAddOn(getRightAddOn(), type);
	}
	public void removeLeftAddOn() {
		getLeftAddOn().removeAllChildren();
	}
	public void removeRightAddOn(IconTypes type) {
		getRightAddOn().removeAllChildren();
	}
	
	@Override
	public InputElement getBaseElement() {
		return getElement().getChild(1).cast();
	}
	protected Element getLeftAddOn(){
		return getElement().getFirstChildElement();
	}
	protected Element getRightAddOn(){
		return getElement().getLastChild().cast();
	}
		
	protected final static void setAddOn(Element span, IconTypes type){
		Element i = span.getFirstChildElement();
		if(i==null) {
			i = Document.get().createElement("i");
			span.setClassName(DEFAULT_GROUP_ADD_ON_STYLE);
			span.appendChild(i);
		}
		i.setClassName(type.getFullValue());
	}
}
